package com.hundsun.aitest.util;
import com.hundsun.aitest.model.testanalysistree.LeafNode;
import com.hundsun.aitest.model.testanalysistree.Node;

import java.util.*;

public class IndentationParser {
    public static List<LeafNode> parse(String text) {
        List<String> lines = Arrays.asList(text.split("\n"));
        Stack<Node> stack = new Stack<>();
        Node root = null;

        for (String line : lines) {
            int indentLevel = 0;
            while (indentLevel < line.length() && line.charAt(indentLevel) == '\t') {
                indentLevel++;
            }

            String content = line.substring(indentLevel).trim();

            if (content.isEmpty()) continue; // Skip empty lines

            while (stack.size() > indentLevel) {
                stack.pop();
            }

            Node node = new Node(content);

            if (stack.isEmpty()) {
                root = node;
            } else {
                stack.peek().addChild(node);
            }

            stack.push(node);
        }

        return root != null ? root.getLeafNodes() : Collections.emptyList();
    }
}
